package com.sbox.widget;

import java.util.ArrayList;
import java.util.List;

import com.sbox.album.R;
import com.sbox.album.model.PictureInfo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

/**
 * class PicListView
 * @author yunzed
 *
 * This class is used to display a list of picture files.
 * The displayed information include:
 * 		FileName
 * 		FileDate
 * 		FileSize
 * 		Width
 * 		Height
 */
public class PicListView extends ListView {
	public PicListView(Context context) {
		super(context);
		init();
	}

	public PicListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}


	public PicListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);		
		init();
	}
	
	private void init() {		
	}
	
	public class ImageHolder
	{
		public RelativeLayout mSmallView;
	}
	
	public class PictureAdapter extends BaseAdapter {
		private List<PictureInfo> mPictures = new ArrayList<PictureInfo>();
		private Context mContext = null;
		private LayoutInflater mInflater = null;
		
		public PictureAdapter(Context c)
		{
			mContext = c;
			mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
			
		public void addPictures(PictureInfo[] pics)
		{
			for( PictureInfo pic : pics )
				mPictures.add(pic);
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
	
			return mPictures.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mPictures.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {		
			PictureInfo pic = null;
			RelativeLayout smallView = null;
			ImageHolder holder = null;
					
			pic = mPictures.get(position);
			if( pic == null )
				return null;
			
			if( convertView != null )
			{				
				holder = (ImageHolder)convertView.getTag();				
				smallView = holder.mSmallView;		
				
			}
			else
			{			
				smallView = (RelativeLayout)mInflater.inflate(R.layout.pic_item, null);
				
				holder = new ImageHolder();
				holder.mSmallView = smallView;
				
				smallView.setTag(holder);
			}
			
			return smallView;
		}	
	}
}
